<?php

namespace App\Repository;

use App\Entity\Chat;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
 * @method Chat|null find($id, $lockMode = null, $lockVersion = null)
 * @method Chat|null findOneBy(array $criteria, array $orderBy = null)
 * @method Chat[]    findAll()
 * @method Chat[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ChatRepository extends ServiceEntityRepository
{
    private $userRepository;
    public function __construct(ManagerRegistry $registry,UserRepository $userRepository)
    {
        parent::__construct($registry, Chat::class);
        $this->userRepository = $userRepository;
    }


    public function chatSingle(User $from,int $to,$content,$flag = 1)
    {
        if ($toUser = $this->userRepository->find($to)) {
            $toId = $to;
        }else{
            $toId = 0;
        }
        $chat = new Chat();
        $chat->setFromUser($from)
            ->setToUser($toUser)
            ->setContent($content)
            ->setLink($this->createLink($from->getId(),$toId))
            ->setFlag($flag)
            ->setStatus(0)
            ->setCreateAt(new \DateTime());
        $this->_em->persist($chat);
        $this->_em->flush();
    }
    public function chatSingleReply(int $from,User $to,$content,$flag = 1)
    {
        if ($fromUser = $this->userRepository->find($from)) {
            $toId = $from;
        }else{
            $toId = 0;
        }
        $chat = new Chat();
        $chat->setFromUser($fromUser)
            ->setToUser($to)
            ->setContent($content)
            ->setLink($this->createLink($to->getId(),$toId))
            ->setFlag($flag)
            ->setStatus(1)
            ->setCreateAt(new \DateTime());
        $this->_em->persist($chat);
        $this->_em->flush();
    }

    public function getLatest(int $from,int $to)
    {
        $link = $this->createLink($from,$to);
        $lastOne = $this->findOneBy(array("link"=>$link),array("id"=>"DESC"));
        if (!$lastOne) {
            return 0;
        }
        return $lastOne->getId();
    }

    public function getList(int $from,int $to,$flag,$lastId = 0)
    {
        $link = $this->createLink($from,$to);
        $qb = $this->createQueryBuilder("A");
        $qb->where("A.link = '$link'");
        $qb->andWhere("A.flag = $flag");

        if ($lastId) {
            $qb->andWhere("A.id < $lastId");
        }
        $qb->orderBy("A.id","DESC");
        $qb->setFirstResult(0)->setMaxResults(10);
        return $qb->getQuery()->execute();
    }

    public function createLink($form,$to)
    {//一对一聊天的场景
        if ($form < $to) {
            return "[{$form}][{$to}]";
        }else{
            return "[{$to}][{$form}]";
        }
    }


    // /**
    //  * @return Chat[] Returns an array of Chat objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('c.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Chat
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
